package com.leapp.sk.viewdelegate.classs

import android.content.Intent
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lklibrary.log.LogOperate
import com.example.inter.RecyclerViewRefreshInterface
import com.example.inter.RefreshLoadMoreInferface
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.MyGridViewHeadFoot
import com.example.mvp.view.RecycleViewDivider
import com.example.mvp.view.RefreshRecyclerView
import com.example.mvp.view.SKTopBar
import com.example.util.ISTECHER
import com.leapp.sk.R
import com.leapp.sk.bean.ClassAlbumEntity
import com.leapp.sk.ui.activity.classs.ClassAlbumActivity
import com.leapp.sk.ui.activity.classs.ClassAlbumDetialActivity
import com.leapp.sk.ui.adapter.ClassAlbumAdapter
import com.leapp.sk.view.AddAlbumDialog

/**
 * 班级相册视图托管类
 * @author  chaohaipeng  on 2018/8/6.
 */
class ClassAlbumDelegate : BaseAppDelegate() {
    /**
     * dialog
     */
    private var addAlbumDialog: AddAlbumDialog? = null

    /**
     * 刷新控件接口
     */
    private var listViewRefreshInterface: RecyclerViewRefreshInterface? = null

    /**
     * 班级相册适配器
     */
    private var classAlbumAdapter: ClassAlbumAdapter? = null

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_class_album
    }

    fun initAdapter(mlist: List<ClassAlbumEntity>, refreshLoadMore: RefreshLoadMoreInferface, listener: SwipeRefreshLayout.OnRefreshListener, addAlbumDialog: AddAlbumDialog?) {
        this.addAlbumDialog=addAlbumDialog
        val rv_aca_list = getView<MyGridViewHeadFoot>(R.id.rv_aca_list)
        val srl_aca_swipe = getView<SwipeRefreshLayout>(R.id.srl_aca_swipe)
        srl_aca_swipe.setOnRefreshListener(listener)
        srl_aca_swipe.setColorSchemeColors(ContextCompat.getColor(getActivity(), R.color.color_19AD19))
        classAlbumAdapter = ClassAlbumAdapter(getActivity(), mlist)
        rv_aca_list.adapter = classAlbumAdapter
        rv_aca_list?.setOnItemClickListener { adapterView, view, i, l ->
            val intent = Intent(getActivity(), ClassAlbumDetialActivity::class.java)
            getActivity<ClassAlbumActivity>().startActivity(intent)
        }
}


//    /**
//     * 初始化适配器
//     */
//    fun initAdapter(mlist: List<ClassAlbumEntity>,refreshLoadMore: RefreshLoadMoreInferface,listener:SwipeRefreshLayout.OnRefreshListener,addAlbumDialog:AddAlbumDialog?) {
//        this.addAlbumDialog=addAlbumDialog
//        val rv_aca_list = getView<RefreshRecyclerView>(R.id.rv_aca_list)
//        val srl_aca_swipe = getView<SwipeRefreshLayout>(R.id.srl_aca_swipe)
//        srl_aca_swipe.setOnRefreshListener(listener)
//        srl_aca_swipe.setColorSchemeColors(ContextCompat.getColor(getActivity(), R.color.color_FF5894))
//        val manager = GridLayoutManager(getActivity(),2)
//        classAlbumAdapter = ClassAlbumAdapter(getActivity(), mlist)
//        rv_aca_list.layoutManager=manager
//        rv_aca_list.adapter=classAlbumAdapter
//        rv_aca_list.addItemDecoration(RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL, LKScreenUtil.dp2px(10f), ContextCompat.getColor(getActivity(),R.color.color_FFFFFF)))
//        listViewRefreshInterface = rv_aca_list.listViewRefreshInterface
//        rv_aca_list.refreshLoadMoreInterface=refreshLoadMore
//
//      //  设置表格，根据position计算在该position处1列占几格数据
//        manager.setSpanSizeLookup(object : GridLayoutManager.SpanSizeLookup() {
//            override fun getSpanSize(position: Int): Int {
//                //计算在哪个position时要显示1列数据，即columnCount / 1列 = 4格，即1列数据占满4格
//                return if ( position == classAlbumAdapter!!.itemCount-1) {
//                    2
//                } else {
//                    1}
//            }
//        })
//
//        classAlbumAdapter?.setOnItemClickListener { v, position ->
//            val intent= Intent(getActivity(), ClassAlbumDetialActivity::class.java)
//            getActivity<ClassAlbumActivity>().startActivity(intent)
//
//        }
//
//
//
//    }
/**
 * 停止刷新
 */
fun stopSwipRefresh() {
    val srl_aca_swipe = getView<SwipeRefreshLayout>(R.id.srl_aca_swipe)
    srl_aca_swipe.isRefreshing = false
}

/**
 * 刷新适配器
 */
fun notifyAdapter() {
    //classAlbumAdapter?.notifyDataSetChanged()
    classAlbumAdapter?.notifyDataSetChanged()
    val mList = classAlbumAdapter?.mList
}

/**
 * 设置标题栏
 */
fun setTitle(title: String?) {
    val jtb_acd = getView<SKTopBar>(R.id.jtb_aca)
    if (ISTECHER) {
        jtb_acd.setRightBtnImage(R.mipmap.icon_add_album_manager)
        jtb_acd.setRightBtnImageShow(true)
    }

    if (!TextUtils.isEmpty(title)) {
        jtb_acd.setTitle(title)
    }
    setEvent(jtb_acd)
}

/**
 * 设置设置事件
 */
fun setEvent(topbar: SKTopBar) {
    topbar?.setEventInterface(object : SKTopBar.EventInterface {
        override fun leftOnClick() {
            getActivity<ClassAlbumActivity>().finish()
        }

        override fun rightOnClick() {
            if (ISTECHER) {
                addAlbumDialog?.show()
            }
        }

    })
}



}